package com.travel.server.manager.impl.command.project.data;

import com.travel.common.util.CollectionUtil;
import com.travel.common.util.CommonUtil;
import com.travel.common.util.DateUtil;
import com.travel.server.manager.dto.request.project.data.QueryProjectDataRequest;
import com.travel.server.manager.dto.response.project.data.QueryProjectDataResponse;
import com.travel.server.manager.dto.response.project.data.QueryProjectDataResponse.AnalysisItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.data.ProjectDataDal;
import com.travel.server.manager.model.dto.data.ProjectOrderDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/29 12:08
 * @Desc:
 */
@Component
@Slf4j
public class QueryProjectDataCommand extends AbstractCommand<QueryProjectDataRequest, QueryProjectDataResponse> {

    @Autowired
    private ProjectDataDal projectDataDal;



    @Override
    protected void checkInput(QueryProjectDataRequest request) {
        checkTimeInput(request);
        if(StringUtils.isBlank(request.getDataStartTime()) || StringUtils.isBlank(request.getDataEndTime()) || StringUtils.isBlank(request.getStartTime()) || StringUtils.isBlank(request.getEndTime())){
            throwInvalidParam();
        }
    }

    @Override
    public QueryProjectDataResponse handle(QueryProjectDataRequest request) {
        checkInput(request);

        Date dataStartTime = DateUtil.getDataTime(request.getDataStartTime());
        Date dateEndTime = DateUtil.getDataEndTime(request.getDataEndTime());
        Date startTime = DateUtil.getDataTime(request.getStartTime());
        Date endTime = DateUtil.getDataEndTime(request.getEndTime());
        QueryProjectDataResponse response = new QueryProjectDataResponse();
        Integer shelf = projectDataDal.selectShelf();
        Integer order = projectDataDal.selectOrder(dataStartTime,dateEndTime);
        Integer pay = projectDataDal.selectPay(dataStartTime, dateEndTime);
        String projectName = null;
        if(StringUtils.isNotBlank(request.getProject())){
            projectName = request.getProject();
        }
        List<ProjectOrderDto> projectOrderList = projectDataDal.selectProjectOrder(startTime, endTime, request.getGroup(), projectName, request.getOffset(), request.getLimit());
        int count = projectDataDal.selectProjectOrderCount(request.getGroup(), projectName);

        response.setShelf(shelf + "");
        response.setOrder(order + "");
        response.setBrowse("0");
        response.setCustomer("0");
        response.setPay(pay + "");
        response.setVisit("0");
        response.setTotalNum(count);
        List<AnalysisItem> list = new ArrayList<>();
        if(!CollectionUtil.isEmpty(projectOrderList)){
            for(ProjectOrderDto dto : projectOrderList){
                if(dto.getId() != null) {
                    AnalysisItem item = new AnalysisItem();
                    item.setApply(dto.getApplyCount() + "");
                    item.setOrder(dto.getOrderCount() + "");
                    item.setApplyOrder(CommonUtil.getProportion(new BigDecimal(dto.getApplyCount()), new BigDecimal(dto.getOrderCount())));
                    item.setBrowse("0");
                    item.setConsul(dto.getConsult());
                    item.setExposure("0");
                    item.setId(dto.getId());
                    item.setProject(dto.getName());
                    item.setVisit("0");
                    item.setVisitApply("0%");
                    list.add(item);
                }
            }
        }
        response.setAnalysisList(list);
        return response;
    }
}
